﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MasterListCreator.Classes
{
    class ExcelHelper
    {
        /// <summary>
        /// Converts Column Index To Name.
        /// </summary>
        /// <param name="index">Column Index to convert.</param>
        /// <returns>Name of Column.</returns>
        public static string ColumnIndexToName(int index)
        {
            string range = string.Empty;
            if (index < 0)
            {
                return range;
            }

            for (int i = 1; index + i > 0; i = 0)
            {
                range = ((char)(65 + index % 26)).ToString() + range;
                index /= 26;
            }

            if (range.Length > 1)
            {
                range = ((char)((int)range[0] - 1)).ToString() + range.Substring(1);
            }

            return range;
        }

        public static string GetAddress(int startCol, int startRow, int endCol, int endRow)
        {
            return GetAddress(ColumnIndexToName(startCol), startRow, ColumnIndexToName(endCol), endRow);
        }

        public static string GetAddress(string startCol, int startRow, string endCol, int endRow)
        {
            return string.Format("{0}{1}:{2}{3}", startCol, startRow, endCol, endRow);
        }
    }
}
